<template>
  <div class="app-container">
    <div class="search">
      <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
        <el-row type="flex" style="flex-wrap:wrap;width:82%;">
          <el-col class="cols">
            <el-form-item prop="userName">
              <el-input v-model="queryParams.userName" placeholder="请输入用户信息" clearable
                        @keyup.enter.native="getList(1)"/>
            </el-form-item>
          </el-col>
          <el-col class="cols">
            <el-form-item prop="toke">
              <el-input v-model="queryParams.toke" placeholder="请输入用户验证" clearable
                        @keyup.enter.native="getList(1)"/>
            </el-form-item>
          </el-col>
          <el-col class="cols">
            <el-form-item prop="urlUpdate">
              <el-input v-model="queryParams.urlUpdate" placeholder="请输入修改地址" clearable
                        @keyup.enter.native="getList(1)"/>
            </el-form-item>
          </el-col>
          <el-col class="cols">
            <el-form-item prop="urlAdd">
              <el-input v-model="queryParams.urlAdd" placeholder="请输入添加地址" clearable
                        @keyup.enter.native="getList(1)"/>
            </el-form-item>
          </el-col>
          <el-col class="cols">
            <el-form-item prop="urlDelect">
              <el-input v-model="queryParams.urlDelect" placeholder="请输入删除地址" clearable
                        @keyup.enter.native="getList(1)"/>
            </el-form-item>
          </el-col>
          <el-col class="cols">
            <el-form-item prop="plattform">
              <el-input v-model="queryParams.plattform" placeholder="平台名" clearable
                        @keyup.enter.native="getList(1)"/>
            </el-form-item>
          </el-col>
        </el-row>

        <div class="btns">
          <!--刷新-->
          <div class="wrap" @click="getList()">
            <img src="../../../assets/images/refresh.png"/>
          </div>

          <!--重置-->
          <div class="wrap" @click="resetQuery">
            <img src="../../../assets/images/search.png"/>
          </div>
        </div>
      </el-form>
    </div>
    <!--        <el-row :gutter="10" class="mb8">-->
    <!--          <el-col :span="1.5">-->
    <!--            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>-->
    <!--          </el-col>-->
    <!--          <el-col :span="1.5">-->
    <!--            <el-button type="success" plain icon="el-icon-edit" size="mini" :disabled="single"-->
    <!--                       @click="handleUpdate">修改-->
    <!--            </el-button>-->
    <!--          </el-col>-->
    <!--          <el-col :span="1.5">-->
    <!--            <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multiple"-->
    <!--                       @click="handleDelete">删除-->
    <!--            </el-button>-->
    <!--          </el-col>-->
    <!--          <el-col :span="1.5">-->
    <!--            <el-button type="primary" plain icon="el-icon-edit" size="mini" @click="tokeinst">生成用户验证</el-button>-->
    <!--          </el-col>-->
    <!--          <el-col :span="1.5">-->
    <!--            <el-button type="warning" plain icon="el-icon-download" size="mini" @click="handleExport">导出</el-button>-->
    <!--          </el-col>-->
    <!--          <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>-->
    <!--        </el-row>-->

    <!--工具栏-->
    <div class="tool">
      <span class="tag">API渠道管理</span>
      <div class="tool-btn">
        <div class="tool-btn-item" @click="suppplyConfirm(1)" style="margin-left: 10px;">批量上架</div>
        <div class="tool-btn-item" @click="suppplyConfirm(2)" style="margin:0 10px;">批量下架</div>
        <el-button style="background: #3A84FF;" type="primary" @click="handleAdd">新增渠道<i
          class="el-icon-plus el-icon--right"></i></el-button>
      </div>
    </div>

    <el-table v-loading="loading" :data="apiList" @selection-change="handleSelectionChange" ref="table">
      <el-table-column type="selection" width="55" align="center"/>

      <el-table-column label="用户信息" align="center" prop="userName" min-width="80"/>
      <el-table-column label="用户验证" align="center" prop="toke" min-width="150">
        <template v-slot="scope">
          <el-popover trigger="hover" placement="top" class="custom-popover">
            <div v-if="scope.row.toke != null">
              <shelf-number-row v-for="(line, index) in scope.row.toke.split(/\n/g)" :key="index" :line="line"/>
            </div>
            <div slot="reference" class="hover-slot-line">{{ scope.row.toke }}
            </div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="修改地址" align="center" min-width="80">
        <template v-slot="scope">
          <el-popover trigger="hover" placement="top" class="custom-popover">
            <div v-if="scope.row.urlUpdate != null">
              <shelf-number-row v-for="(line, index) in scope.row.urlUpdate.split(/\n/g)" :key="index" :line="line"/>
            </div>
            <div slot="reference" class="hover-slot-line">{{ scope.row.urlUpdate }}</div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="添加地址" align="center" min-width="160">
        <template v-slot="scope">
          <el-popover trigger="hover" placement="top" class="custom-popover">
            <div v-if="scope.row.urlAdd != null">
              <shelf-number-row v-for="(line, index) in scope.row.urlAdd.split(/\n/g)" :key="index" :line="line"/>
            </div>
            <div slot="reference" class="hover-slot-line">{{ scope.row.urlAdd }}</div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="删除地址" align="center" prop="urlDelect" min-width="160">
        <template v-slot="scope">
          <el-popover trigger="hover" placement="top" class="custom-popover">
            <div v-if="scope.row.urlDelect != null">
              <shelf-number-row v-for="(line, index) in scope.row.urlDelect.split(/\n/g)" :key="index" :line="line"/>
            </div>
            <div slot="reference" class="hover-slot-line">{{ scope.row.urlDelect }}</div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="100">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" active-color="#13ce66" inactive-color="#ff4949" active-value="1"
                     inactive-value="0" @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="查询地址" align="center" prop="seleUrl" min-width="100">
        <template v-slot="scope">
          <el-popover trigger="hover" placement="top" class="custom-popover">
            <div v-if="scope.row.seleUrl != null">
              <shelf-number-row v-for="(line, index) in scope.row.seleUrl.split(/\n/g)" :key="index" :line="line"/>
            </div>
            <div slot="reference" class="hover-slot-line">{{ scope.row.seleUrl }}</div>
          </el-popover>
        </template>
      </el-table-column>
      <el-table-column label="个人系统名" align="center" prop="gmUserName" min-width="80"/>
      <el-table-column label="平台名" align="center" prop="plattform" min-width="80"/>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width" width="180">
        <template slot-scope="scope">
          <el-button size="mini" type="text" @click="handleSetting(scope.row)">
            <span style="color: rgba(83, 90, 244, 1);">推送配置</span>
          </el-button>
          <el-button size="mini" type="text" @click="handleUpdate(scope.row)">
            <span style="color: rgba(83, 90, 244, 1);">修改</span>
          </el-button>
          <el-button size="mini" type="text" @click="handleDelete(scope.row)">
            <span style="color: rgba(83, 90, 244, 1);">删除</span>
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination class="pagination" v-show="total > 0" :total="total" :page.sync="queryParams.pageNum"
                :limit.sync="queryParams.pageSize" @pagination="getList('')"/>

    <!-- 修改对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" label-width="100px">
        <el-form-item label="平台名" prop="plattform" :rules="{ required: true, message: '平台名称不能为空！' }">
          <el-input v-model="form.plattform" placeholder="请输入平台名"/>
        </el-form-item>

        <el-form-item label="用户信息" prop="userName" :rules="{ required: true, message: '用户信息不能为空！' }">
          <el-input v-model="form.userName" placeholder="请输入用户信息"/>
        </el-form-item>

        <el-form-item label="用户验证" prop="toke">
          <el-input v-model="form.toke" placeholder="请输入用户验证"/>
        </el-form-item>

        <el-form-item label="修改地址">
          <el-input v-model="form.urlUpdate" placeholder="请输入修改地址"/>
        </el-form-item>

        <el-form-item label="添加地址">
          <el-input v-model="form.urlAdd" placeholder="请输入添加地址v"/>
        </el-form-item>

        <el-form-item label="删除地址">
          <el-input v-model="form.urlDelect" placeholder="请输入删除地址"/>
        </el-form-item>

        <el-form-item label="查看地址">
          <el-input v-model="form.seleUrl" placeholder="请输入查看地址"/>
        </el-form-item>

        <el-form-item label="个人系统名">
          <el-input v-model="form.gmUserName" placeholder="请输入个人系统名"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="submitForm">确 定</el-button>
      </div>
    </el-dialog>

    <!-- 用户验证弹框 -->
    <el-dialog :title="title" :visible.sync="gmtoke" width="500px" append-to-body>
      <el-form ref="gmform" :model="gmform" label-width="80px">
        <el-form-item label="用户名" prop="userName">
          <el-input v-model="gmform.userName" placeholder="请输入用户信息"/>
        </el-form-item>
        <el-form-item label="toke" prop="toke">
          <el-input v-model="gmform.toKe" placeholder="toke"/>
        </el-form-item>
        <el-form-item label="用户验证" prop="token">
          <el-input v-model="gmform.token" :suffix-icon="copyIcon" ref="input"/>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="tokeSubmitFormIs">生成toke</el-button>
        <el-button type="primary" @click="gmcancel">确 定</el-button>
        <el-button @click="gmcancel">取 消</el-button>
      </div>
    </el-dialog>

    <!-- 推送配置弹框 -->
    <el-dialog title="推送" :visible.sync="configApi" width="500px" append-to-body>
      <el-form ref="supply" label-width="80px">
        <el-form-item label="供应商">
          <el-select v-model="gmform.supplierList" multiple placeholder="请选择供应商">
            <el-option v-for="(item, indx) in suppliersList" :key="indx" :label="item" :value="item"/>
          </el-select>
        </el-form-item>

        <el-form-item label="溢价比例(%)" prop="type">
          <el-input-number v-model="gmform.money" style="width:300px;" placeholder="请输入游戏类型"/>
        </el-form-item>
        <el-form-item label="游戏品类">
          <el-select v-model="gmform.dictCodeIdList" multiple placeholder="请选择供应商">
            <el-option v-for="(item, indx) in gameList" :key="indx" :label="item.dictLabel"
                       :value="item.dictCode + ''"/>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="configApi = false">取 消</el-button>
        <el-button type="primary" @click="submitConfig">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listApi,
  getApi,
  delApi,
  addApi,
  updateApi,
  pullage,
  provingTokce,
  upGrounding,
  downGrounding,
  getSupplier,
  pushConfig
} from '@/api/business/api'
import ShelfNumberRow from "@/components/ShelfNumberRow.vue";
import {getConfig, load} from "runjs/lib/script";

export default {
  name: "Api",
  components: {ShelfNumberRow},
  data() {
    return {
      suppliersList: [],
      supplier: [],
      gameList: [],
      // 遮罩层
      loading: false,
      supplyLoading: false,
      type: '',
      copyIcon: 'el-icon-document', // 复制按钮的图标
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 表格数据
      apiList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 游戏信息弹框
      pullageIs: false,
      // 游戏信息弹框
      gmtoke: false,
      configApi: false,
      // 游戏信息
      gmList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        userName: null,
        toke: null,
        urlUpdate: null,
        urlAdd: null,
        urlDelect: null,
      },
      // 表单参数
      form: {},
      // 表单参数
      gmform: {
        userName: '',//用户信息
        toke: '',//用户验证
        urlUpdate: '',//修改地址
        urlAdd: '',//添加地址
        urlDelect: '',//地址
        gm: '',//游戏类型
        gmUserName: '',//个人系统名
        seleUrl: '',//查看地址
        status: '',//状态
        plattform: '',//平台
        supplierList: [],// 供应商
        dictCodeIdList: [], //游戏平台
        money: '',// 价格百分比
      }
    };
  },
  mounted() {
    this.createCopyButton();
  },
  created() {
    this.getList();
    this.getSuppliers();
    //查询游戏列字典
    this.getDicts('game_category').then(res => {
      this.gameList = res.data
    })
  },

  methods: {
    // 推送配置
    submitConfig() {
      pushConfig(this.gmform).then(res => {
        if (res.code == 200) {
          this.configApi = false;
          this.getList();
        } else {
          this.$message.error(res.msg);
          this.getList();
        }
      })
    },

    // 获取供应商
    getSuppliers() {
      getSupplier().then(res => {
        this.suppliersList = res.data;
      })
    },

    // 提交
    suppplyConfirm(type) {
      let tipTxt = type == 1 ? '上架' : '下架';
      if (this.ids.length == 0) return this.$message.error('请选择一条记录！');
      this.$confirm('此操作将批量' + tipTxt + 'api配置, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        if (type == 1) {
          upGrounding(this.ids).then(res => {
            if (res.code == 200) {
              this.$message.success("上架成功！");
            } else {
              this.$message.success(res.msg)
            }
          }).catch((res) => {
            this.$message.error("上架失败！");
          })
        } else {
          downGrounding(this.ids).then(res => {
            if (res.code == 200) {
              this.$message.success("下架成功！");
            } else {
              this.$message.error(res.msg)
            }
          }).catch((res) => {
            this.$message.error("下架失败！")
          })
        }
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消'
        });
      });
    },

    /** 查询列表 */
    getList() {
      this.loading = true;
      listApi(this.queryParams).then(response => {
        this.apiList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 取消按钮
    gmcancel() {
      this.gmtoke = false;
      this.gmReset();
    },

    // 表单重置
    reset() {
      this.gmform = {
        id: null,
        userName: null,
        toke: null,
        urlUpdate: null,
        urlAdd: null,
        urlDelect: null,
        updateTime: null,
        createTime: null,
        createBy: null,
        supplierList: [],// 供应商
        dictCodeIdList: [], //游戏平台
        money: null,// 价格百分比

      };
      this.resetForm("form");
    },

    // 表单重置
    gmReset() {
      this.gmform = {
        userName: null,
        toke: null,
      };
      this.resetForm("gmform");
    },

    /** 重置按钮操作 */
    resetQuery() {
      this.$refs.queryForm.resetFields();
      this.getList(1);
    },

    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length !== 1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    /** 新增按钮操作 */
    tokeinst() {
      this.gmReset();
      this.gmtoke = true;
    },

    // 推送配置
    handleSetting(row) {
      this.reset();
      const id = row.id || this.ids
      getApi(id).then(response => {
        this.gmform = response.data;
        this.configApi = true;
        this.title = "配置推送信息";
      });
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getApi(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改";
      });
    },

    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateApi(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addApi(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },

    /** toke提交 */
    tokeSubmitFormIs() {
      this.$refs["gmform"].validate(valid => {
        if (valid) {
          if (this.gmform != null) {
            provingTokce(this.gmform).then(response => {
              this.$modal.msgSuccess("生成成功");
              this.gmform.token = response.data.token;
              this.getList();
            });
          }
        }
      });
    },

    SubmitFormIs() {
      this.$refs["gmform"].validate(valid => {
        if (valid) {
          if (this.gmform != null) {
            provingTokce(this.gmform).then(response => {
              this.$modal.msgSuccess("生成成功");
              this.gmform.token = response.data.token;
              this.getList();
            });
          }
        }
      });
    },

    handleStatusChange(row) {
      updateApi(row).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.open = false
        this.getList();
      })
    },

    /** 查询游戏信息 */
    pullage(row) {
      pullage(row).then((date) => {
        if (date.code == 200) {
          this.gmList = date.data
          this.pullageIs = true
          console.log(this.gmList)
        } else {
          this.$message.error(date.msg);
        }
      })
    },

    createCopyButton() {
      const copyButton = document.createElement('i');
      copyButton.className = 'el-icon-caret-bottom el-input__icon el-icon-copy';
      copyButton.title = '复制';
      copyButton.style.cursor = 'pointer';
      copyButton.setAttribute('aria-hidden', 'true');
      copyButton.addEventListener('click', this.handleCopy);
      // this.$refs.input.$el.querySelector('.el-input__inner').appendChild(copyButton);
    },

    handleCopy() {
      const inputElement = this.$refs.input.$refs.input;
      inputElement.select();
      document.execCommand('copy');
      inputElement.blur();
      this.$message.success('复制成功！');
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除编号为"' + ids + '"的数据项？').then(function () {
        return delApi(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/api/export', {
        ...this.queryParams
      }, `api_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
<style lang="scss" scoped>
.app-container {
  background: #F3F5FF;
  padding: 30px;

  /*搜索*/
  .search {
    border-radius: 16px;
    overflow: hidden;
    margin-bottom: 20px;

    ::v-deep.el-form {
      display: flex;
      justify-content: space-between;
      align-items: center;
      padding: 20px 20px 0px;
      background: #3A84FF;

      .cols {
        margin: 0 10px 10px 0;
      }
    }

    .el-col {
      width: 200px;
    }

    ::v-deep.el-form-item {
      margin-bottom: 0 !important;
    }

    ::v-deep.el-input__inner {
      background: #fff;
      border: 0;
      font-size: 12px;
      width: 200px;
      height: 38px;
      color: #0060FF;
    }

    ::v-deep.el-input {
      border-radius: 8px;
      background: #FAFBFC;
      border: 0.5px solid #A2B3D0;
      overflow: hidden;
      width: 200px;
      height: 38px;
    }

    .btns {
      display: flex;
      align-self: flex-start;

      ::v-deep.el-button {
        background-color: #fff;
        margin-left: 12px;
        color: #3380FD;

        .el-icon-plus {
          &::after {
            content: "";
            font-weight: bold;
          }
        }
      }

      .wrap {
        display: flex;
        justify-content: center;
        align-items: center;
        width: 38px;
        height: 38px;
        border-radius: 8px;
        background: #FFFFFF;
        border: 1px solid #116BFF;
        margin-left: 12px;
        cursor: pointer;
      }
    }

    /*radio*/
    .el-radio-group {
      width: 100%;
      padding: 20px 20px 10px;
      background: rgba(255, 255, 255, 0.7);

      .el-radio {
        margin-bottom: 8px;
      }

      ::v-deep.el-radio__inner {
        width: 20px;
        height: 20px;
        border-radius: 4px;
        background: #FAFBFC;
        border: 1px solid #D8E4EF;
        overflow: hidden;
        text-align: center;
        line-height: 32px;

        &::after {
          content: "☑️";
          display: block;
          color: #1890FF;
          font-size: 20px;
          width: 30px;
          height: 30px;
          background: #fff;
        }
      }
    }
  }

  .tool {
    display: flex;
    align-items: center;
    justify-content: space-between;
    margin-bottom: 20px;

    .tag {
      font-size: 18px;
      color: rgba(17, 38, 60, 1);
      border-left: 3px solid rgba(84, 91, 245, 1);
      padding-left: 10px;
    }

    .tool-btn {
      display: flex;

      .tool-btn-item {
        width: 96px;
        height: 36px;
        line-height: 36px;
        border-radius: 8px;
        background: #FFFFFF;
        border: 1px solid #116BFF;
        font-family: Source Han Sans;
        font-size: 14px;
        font-weight: normal;
        text-align: center;
        color: #2376FE;
        cursor: pointer;

        &:hover {
          background: rgba(17, 107, 255, .01);
        }
      }
    }
  }

  /*列表*/
  ::v-deep.el-table {
    border-radius: 16px;

    th {
      height: 50px;
      background: #488DFE;
      font-family: Source Han Sans SC;
      font-size: 14px;
      color: #fff;
      font-weight: normal;
      border-right: 1px solid #E2E6F5;
    }

    td {
      height: 50px;
      font-family: Source Han Sans SC;
      font-size: 14px;
      color: #3D3D3D;
      font-weight: normal;
      border-right: 1px solid #E2E6F5;
    }

    .el-table__body tr {
      &:nth-child(odd) {
        background-color: #FEFEFE;
      }

      &:nth-child(even) {
        background-color: #F8FAFE;
      }
    }

    .el-table__body-wrapper {
      &::-webkit-scrollbar {
        height: 10px;
      }

      &::-webkit-scrollbar-track {
        background-color: #E4E8F5;
      }

      &::-webkit-scrollbar-thumb {
        border-radius: 10px;
        background-color: #3380FD;
      }
    }

    .status {
      display: inline-block;
      width: 54px;
      height: 26px;
      line-height: 26px;
      text-align: center;
      opacity: 0.8;
      border-radius: 4px;
      font-size: 12px;
      transition: opacity 0.3s ease-in-out;
    }

    .normal {
      color: #00B3A7;
      background-color: #E0F7FA;
    }

    .tradingIn {
      color: #FFA500;
      background-color: #ffeac9;
    }

    .soldOut {
      color: #FF0000;
      background-color: #ffdada;
    }

    .abnormal {
      color: #FF00FF;
      background-color: #FFE4FF;
    }

    .cancelled {
      color: #a19f9f;
      background-color: #ececec;
    }
  }

  /*分页*/
  .pagination {
    background-color: transparent !important;

    ::v-deep.el-pagination {
      display: flex;
      align-items: center;

      .number {
        width: 40px;
        height: 40px;
        line-height: 40px;
        background-color: #fff;
        border-radius: 8px;
      }

      .el-pager > .el-icon {
        height: 40px;
        width: 40px;
        background: #fff;
        line-height: 40px;
        border-radius: 8px;
      }
    }
  }
}
</style>
